//
//  ChatViewController.m
//  YouinLiveDemo
//
//  Created by lprison on 2021/9/4.
//

#import "ChatViewController.h"
#import "ChatTextCell.h"
#import "ChatImageCell.h"
#import "TOCropViewController.h"
#import "cameraORphotoalbum.h"
#import "ZXMessageCase.h"

@interface ChatViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate,cameraORphotoalbumDelegate,TOCropViewControllerDelegate,youinRTCChatDelegate>
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UIButton *sendButton;
@property (nonatomic,strong) UIButton *picButton;
@property (nonatomic,strong) cameraORphotoalbum *corp;
@property (nonatomic,strong) NSString *filePath;



@end

@implementation ChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self setTabLe];
//    self.messageArray = [NSMutableArray new];
    
    self.textField = [[UITextField alloc] init];
    self.textField.placeholder = @"请输入";
    self.textField.layer.borderWidth = 1;
    self.textField.layer.borderColor = UIColor.lightGrayColor.CGColor;
    if (@available(iOS 13.0, *)) {
        self.textField.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    } else {
        // Fallback on earlier versions
    }
    [self.view addSubview:self.textField];
    
    self.picButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.picButton setTitle:@"图片" forState:UIControlStateNormal];
    self.picButton.backgroundColor = [UIColor lightGrayColor];
    [self.picButton addTarget:self action:@selector(picActtion) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.picButton];
    
    self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sendButton setTitle:@"发送" forState:UIControlStateNormal];
    self.sendButton.backgroundColor = [UIColor lightGrayColor];
    [self.sendButton addTarget:self action:@selector(sendAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sendButton];
    
    _corp = [[cameraORphotoalbum alloc]init];
    _corp.delegate = self;
    
    [YouinRTCManager shardManager].chatDelegate = self;
//    [self getHistoryMessage];
    
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
//    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.mas_equalTo(self.view);
//    }];
//
//    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.view).offset(16);
//        make.height.mas_equalTo(30);
//        make.bottom.mas_equalTo(self.view).offset(-20);
//        make.top.mas_equalTo(self.tableView.mas_bottom).offset(10);
//    }];
//
//    [self.picButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.textField.mas_right).offset(20);
//        make.height.mas_equalTo(30);
//        make.width.mas_equalTo(50);
//        make.top.mas_equalTo(self.textField);
//
//    }];
//
//    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.picButton.mas_right).offset(5);
//        make.height.mas_equalTo(30);
//        make.width.mas_equalTo(50);
//        make.right.mas_equalTo(self.view).offset(-16);
//        make.top.mas_equalTo(self.textField);
//    }];
}

//-(void)setTabLe{
//
//    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
//
//    _tableView.delegate = self;
//    _tableView.dataSource = self;
//    _tableView.showsVerticalScrollIndicator = false;
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    _tableView.estimatedRowHeight = 100;
//    _tableView.rowHeight = UITableViewAutomaticDimension;
//    [_tableView registerClass:[ChatTextCell class] forCellReuseIdentifier:@"ChatTextCell"];
//    [_tableView registerClass:[ChatImageCell class] forCellReuseIdentifier:@"ChatImageCell"];
//    _tableView.tableFooterView = [UIView new];
//    [self.view addSubview:_tableView];
//
//    if (@available(iOS 11.0, *)) {
//        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//    }else {
//        self.automaticallyAdjustsScrollViewInsets = NO;
//    }
//
//    [self.view addSubview:_tableView];
//}



-(void)picActtion{
    
//    if(([self.model._lcattrs.type isEqualToString:@"forbidden_all"] || self.model._lcattrs.userId == [YouinSDKManager shardManager].conversation.clientId) && self.model._lcattrs.status){
//        [[ZXMessageCase shareInstace] showMessage:@"已被禁言"];
//        return;
//    }
    
    [_corp showview];
}


-(void)manageData{
    
    
}

-(void)sendAction{
    
 
    
    [[YouinRTCManager shardManager] sendTextMessage:self.textField.text callback:^(BOOL, NSError * _Nullable) {
        self.textField.text = @"";
    }];
    
}
//
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.messageArray.count;
//
//}
//
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if(self.messageArray[indexPath.row].messageType == -1){
//        ChatTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChatTextCell" forIndexPath:indexPath];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.message = self.messageArray[indexPath.row];
//        return  cell;
//    }else if(self.messageArray[indexPath.row].messageType == -2){
//        ChatImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChatImageCell" forIndexPath:indexPath];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.message = self.messageArray[indexPath.row];
//        return  cell;
//    }
//
//    return [UITableViewCell new];
//
//}
//
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//
//}
//
//#pragma mark - JXCategoryListContentViewDelegate
//
///**
// 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
// */
//- (UIView *)listView {
//    return self.view;
//}
//
//#pragma mark - pic delegate
//
//-(void)cameraORphotoalbumDelegatedidSelectButton:(UIButton *)didSelectButton{
//    if ([didSelectButton.titleLabel.text isEqualToString:@"拍照"]) {
//        //相机
//        [self takePhoto];
//    } else if ([didSelectButton.titleLabel.text isEqualToString:@"相册"]){
//        //相册
//        [self imageBtnClick];
//    }else if([didSelectButton.titleLabel.text isEqualToString:@""]){
//        // UIImageWriteToSavedPhotosAlbum(self.header.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//    }
//}
//
//-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
//
//}
//
////开始拍照
//-(void)takePhoto{
//    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
//    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
//        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//        picker.delegate = self;
//        //设置拍照后的图片可被编辑
//        picker.allowsEditing = NO;
//        picker.sourceType = sourceType;
//        picker.modalPresentationStyle = UIModalPresentationFullScreen;
//        [self presentViewController:picker animated:YES completion:^{
//
//        }];
//    }else{
//
//    }
//}
//-(void)imageBtnClick{
//    UIImagePickerController *photo = [[UIImagePickerController alloc] init];
//    photo.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    photo.delegate = self;
//    photo.allowsEditing = NO;
//    photo.view.backgroundColor=[UIColor whiteColor];
//    photo.modalPresentationStyle = UIModalPresentationFullScreen;
//    [self presentViewController:photo animated:YES completion:nil];
//}
////当选择一张图片进入
//-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//    [picker dismissViewControllerAnimated:YES completion:^{
//
//    }];
//    UIImage *smallimage;
//    if (picker.allowsEditing == YES) {
//        smallimage  = [info objectForKey:@"UIImagePickerControllerEditedImage"];
//    }else{
//        smallimage  = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//    }
//
//    TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:smallimage];
//    cropViewController.delegate = self;
//    cropViewController.aspectRatioPreset = TOCropViewControllerAspectRatioPresetSquare;
//    cropViewController.toolbar.rotateClockwiseButtonHidden = YES;
//    cropViewController.toolbar.clampButtonHidden = YES;
//    cropViewController.toolbar.rotateCounterclockwiseButtonHidden = YES;
//    cropViewController.toolbar.resetButtonHidden = YES;
//    cropViewController.aspectRatioLockEnabled = YES;
//    cropViewController.modalPresentationStyle = UIModalPresentationFullScreen;
//    [self presentViewController:cropViewController animated:NO completion:nil];
//}
//
//- (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle
//{
//
//
//
//    [[YouinRTCManager shardManager] sendImageMessage:image callback:^(BOOL, NSError * _Nullable) {
//
//    }];
//
//
//    [cropViewController dismissViewControllerAnimated:NO completion:nil];
//
//
//
//}
//
//
//
//-(void)onNewMessageRecive:(BaseMessageModel *)messageModel{
//    // messageType -1  文字信息  -2 图片信息
//    [self.messageArray addObject:messageModel];
//    [self.tableView reloadData];
//    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.messageArray.count-1 inSection:0]
//   animated:YES
//     scrollPosition:UITableViewScrollPositionMiddle];
//}
//
//-(void)getHistoryMessage{
//    [[YouinRTCManager shardManager] getHistoryMessage:100 callback:^(NSArray<BaseMessageModel *> * _Nonnull message, NSError * _Nullable error) {
//        if(!error){
//            [self.messageArray addObjectsFromArray:message];
//            [self.tableView reloadData];
//            if(self.messageArray.count > 0){
//                [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.messageArray.count-1 inSection:0]
//               animated:YES
//                 scrollPosition:UITableViewScrollPositionMiddle];
//            }
//
//        }
//    }];
//}


@end
